<?php

Class Model_LSI_Word {
	const TABLE = "lsi_word";

	/**
	 * @var protected $word word value
	 * @method string get_word() get_word(void)
	 * @method void set_word() set_word(string $word)
	 */
	private $word;

	/**
	 * @var protected $context context value
	 * @method string get_context() get_context(void)
	 * @method void set_context() set_context(string $context)
	 */
	private $context;

	/**
	 * @var protected $count count value
	 * @method int get_count() get_count(void)
	 * @method void set_count() set_count(int $count)
	 */
	public $count = "0";

	/**
	 * @var protected $relevant relevant value
	 * @method string get_relevant() get_relevant(void)
	 * @method void set_relevant() set_relevant(string $relevant)
	 */
	public $relevant = "yes";

	/**
	 * @return Model_Lsi_word
	 */
	public function __construct($word, $context) {
		$this->word = $word;
		$this->context = $context;
		if($obj = DB::select("count", "relevant")->from(self::TABLE)->where("word", "=", $word)->where("context", "=", $context)->limit(1)->as_object()->execute()->current()) {
			$this->count = $obj->count;
			$this->relevant = $obj->relevant;
		}
	}

	public function save() {
		if (1 >= $this->count) {
			$db = DB::insert(self::TABLE, array("word", "context", "count", "relevant"))->values(array($this->word, $this->context, 1, $this->relevant));
		} else {
			$db = DB::update(self::TABLE)->set(array("count" => $this->count, "relevant" => $this->relevant))->where("word", "=", $this->word)->where("context", "=", $this->context);
		}
		$db->execute();
	}

}